package com.cskaoyan.aspectj;

import com.cskaoyan.bean.MarketAdmin;
import com.cskaoyan.bean.MarketLog;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.vo.AdminAdminUpdateVo;
import com.cskaoyan.service.LogService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

import static com.cskaoyan.utils.HttpUtil.addLog;

/**
 * @Author: JianhuaZeng
 * @Date: 2022/12/30 22:07
 */
@Component
@Aspect
public class AdminLogAspect {

    @Autowired
    LogService logService;

    private String getAdmin() {
        Subject subject = SecurityUtils.getSubject();
        if (!subject.isAuthenticated()) {
            return "";
        }
        MarketAdmin principal = (MarketAdmin) subject.getPrincipal();
        return principal.getUsername();
    }

    @Pointcut("@annotation(com.cskaoyan.aspectj.admin.AdminLogin)")
    public void pointcutAdminLogin() {
    }

    @Pointcut("@annotation(com.cskaoyan.aspectj.admin.AdminLogout)")
    public void pointcutAdminLogout() {
    }

    @Pointcut("@annotation(com.cskaoyan.aspectj.admin.AdminCreate)")
    public void pointcutAdminCreate() {
    }

    @Pointcut("@annotation(com.cskaoyan.aspectj.admin.AdminDelete)")
    public void pointcutAdminDelete() {
    }

    @Pointcut("@annotation(com.cskaoyan.aspectj.admin.AdminUpdate)")
    public void pointcutAdminUpdate() {
    }

    /**
     * @description: 登录
     * @param: joinPoint
     * @return: java.lang.Object
     * @author: JianhuaZeng
     * @date: 2023/1/2 2023/1/2
     */
    @Around("pointcutAdminLogin()")
    public Object adminLoginLog(ProceedingJoinPoint joinPoint) throws Throwable {
        Object res = joinPoint.proceed();
        BaseRespVo respVo = (BaseRespVo) res;
        String errmsg = respVo.getErrmsg();
        MarketLog log = addLog(res, getAdmin(), "登录", errmsg);
        logService.insert(log);
        return res;
    }

    /**
     * @description: 退出
     * @param: joinPoint
     * @return: java.lang.Object
     * @author: JianhuaZeng
     * @date: 2023/1/2 2023/1/2
     */
    @Around("pointcutAdminLogout()")
    public Object adminLogoutLog(ProceedingJoinPoint joinPoint) throws Throwable {
        String admin = getAdmin();
        Object res = joinPoint.proceed();
        MarketLog log = addLog(res, admin, "退出", null);
        logService.insert(log);
        return res;
    }

    /**
     * @description: 添加管理员
     * @param: joinPoint
     * @return: void
     * @author: JianhuaZeng
     * @date: 2023/1/2 2023/1/2
     */
    @Around("pointcutAdminCreate()")
    public Object adminCreateLog(ProceedingJoinPoint joinPoint) throws Throwable {
        Object res = joinPoint.proceed();
        Object[] args = joinPoint.getArgs();
        MarketAdmin marketAdmin = (MarketAdmin) args[0];
        String username = marketAdmin.getUsername();
        MarketLog log = addLog(res, getAdmin(), "添加管理员", username);
        logService.insert(log);
        return res;
    }

    /**
     * @description: 删除管理员
     * @param: joinPoint
     * @return: java.lang.Object
     * @author: JianhuaZeng
     * @date: 2023/1/2 2023/1/2
     */
    @Around("pointcutAdminDelete()")
    public Object adminDeleteLog(ProceedingJoinPoint joinPoint) throws Throwable {
        Object res = joinPoint.proceed();
        Object[] args = joinPoint.getArgs();
        Map map = (Map) args[0];
        String username = (String) map.get("username");
        MarketLog log = addLog(res, getAdmin(), "删除管理员", username);
        logService.insert(log);
        return res;
    }

    /**
     * @description: 更新管理员
     * @param: joinPoint
     * @return: java.lang.Object
     * @author: JianhuaZeng
     * @date: 2023/1/2 2023/1/2
     */
    @Around("pointcutAdminUpdate()")
    public Object adminUpdateLog(ProceedingJoinPoint joinPoint) throws Throwable {
        Object res = joinPoint.proceed();
        BaseRespVo<AdminAdminUpdateVo> respVo = (BaseRespVo<AdminAdminUpdateVo>) res;
        String username = respVo.getData().getUsername();
        MarketLog log = addLog(res, getAdmin(), "编辑管理员", username);
        logService.insert(log);
        return res;
    }
}
